package labb1.del2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Mikko Puustinen
 * @version 0.1, 2007-sep-12
 */
public class Deck {
    private List<Card> theCards;

    /**
     * Creates a standard deck with 52 cards and shuffles it.
     */
    public Deck() {
        theCards = new ArrayList<Card>();
        for (int suit = Cards.SPADES; suit <= Cards.DIAMONDS; suit++)
            for (int value = Cards.ACE_LOW; value <= Cards.KING; value++)
                theCards.add(new Card(value, suit));

        shuffleCards();
    }

    /**
     * Add a card to the <code>Deck</code>.
     *
     * @param card the <code>Card</code> to add.
     */
    public void addCard(Card card) {
        theCards.add(card);
    }

    /**
     * Returns how many <code>Card</code>s there is in the <code>Deck</code>.
     *
     * @return number of elements in <tt>theCards</tt>
     */
    public int getNoOfCards() {
        return theCards.size();
    }

    /**
     * Get the last <code>Card</code> in the <code>Deck</code>.
     *
     * @return the Card to be removed from the deck
     */
    public Card getCard() {
        int card = theCards.size() - 1;
        if (card < 0)
            throw new NoSuchCardException("Deck is empty", card);
        return theCards.remove(card);
    }

    /**
     * Shows the specified card. The lowest value to be used is 1.
     *
     * @param card show this <code>Card</code>
     *
     * @return the <code>Card</code> on the speci
     */
    public Card showCard(int card) {
        if (card < 1 || card > theCards.size())
            throw new NoSuchCardException(card);
        return theCards.get(card - 1);
    }

    /**
     * Shuffles the <code>Deck</code>.
     */
    public void shuffleCards() {
        Collections.shuffle(theCards);
    }

    /**
     * Returns a string representation of the object.
     *
     * @return string representation of the <code>Deck</code>.
     */
    public String toString() {
        return getNoOfCards() + " cards in the deck";
    }
}
